package com.danielepelagatti.flickrmanager.commands.upload
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import com.adobe.crypto.MD5;
	import com.adobe.webapis.flickr.Photo;
	import com.adobe.webapis.flickr.methodgroups.MethodGroupHelper;
	import com.danielepelagatti.flickrmanager.events.PhotoEvent;
	import com.danielepelagatti.flickrmanager.events.UploadEvent;
	import com.danielepelagatti.flickrmanager.model.Flickr;
	import com.danielepelagatti.flickrmanager.model.LocalUser;
	import com.theriabook.utils.Logger;
	
	import flash.events.DataEvent;
	import flash.events.ProgressEvent;
	import flash.filesystem.File;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	
	import mx.utils.StringUtil;
	/**
	 * 
	 * @author dpelagatti
	 * 
	 */
	public class Upload implements ICommand
	{
		/**
		 * 
		 * @param event
		 * 
		 */		
		public function execute(event:CairngormEvent):void
		{
			var upEvent:UploadEvent 		= UploadEvent(event);
			
			var fileReference:FileReference = upEvent.uploadVO.file as FileReference;
			var title:String				= upEvent.uploadVO.title;
			var description:String			= upEvent.uploadVO.description;
			var tags:String					= upEvent.uploadVO.tags;				
			var is_public:Boolean			= upEvent.uploadVO.is_public;
			var is_friend:Boolean			= upEvent.uploadVO.is_friend;
			var is_family:Boolean			= upEvent.uploadVO.is_family;
			
			// The upload method requires signing, so go through
			// the signature process

			// [OvD] Flash sends both the 'Filename' and the 'Upload' values
			// in the body of the POST request, so these are needed for the signature
			// as well, otherwise Flickr returns a error code 96 'invalid signature'
			var sig:String 	 = StringUtil.trim( Flickr.secret );
			sig 			+= "Filename" + fileReference.name;
			sig 			+= "UploadSubmit Query"; //
			sig 			+= "api_key" + StringUtil.trim( Flickr.apiKey);
			sig 			+= "auth_token" + StringUtil.trim( Flickr.token );
			
			// [OvD] optional values, the order is irrelevant
			if ( description != "" ) 	sig += "description" 	+ description;
			if ( is_family ) 			sig += "is_family" 		+ ( is_family ? 1 : 0 );
			if ( is_friend ) 			sig += "is_friend" 		+ ( is_friend ? 1 : 0 );
			if ( is_public ) 			sig += "is_public" 		+ ( is_public ? 1 : 0 );
			if ( tags != "" ) 			sig += "tags" 			+ tags;
			if ( title != "" ) 			sig += "title" 			+ title;
			
			var vars:URLVariables 	= new URLVariables();
			vars.auth_token 		= StringUtil.trim( Flickr.token );
			vars.api_sig 			= MD5.hash( sig );
			vars.api_key 			= StringUtil.trim(  Flickr.apiKey );
			
			// [OvD] optional values, same order as the signature
			if ( description != "" ) 	vars.description 	= description;
			if ( is_family ) 			vars.is_family 		= ( is_family ? 1 : 0 );
			if ( is_friend ) 			vars.is_friend 		= ( is_friend ? 1 : 0 );
			if ( is_public ) 			vars.is_public 		= ( is_public ? 1 : 0 );
			if ( tags != "" ) 			vars.tags 			= tags;
			if ( title != "" ) 			vars.title 			= title;

			var request:URLRequest 	= new URLRequest( "http://api.flickr.com/services/upload/" );
			request.data 			= vars;
			request.method 			= URLRequestMethod.POST;
			
			LocalUser.getInstance().addUpload(upEvent.uploadVO);
			
			//if(fileReference.hasEventListener(DataEvent.UPLOAD_COMPLETE_DATA))
			//	fileReference.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA,result)
			
			fileReference.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,result);
			
			Logger.debug("Executing Upload");
			// [OvD] Flickr expects the filename parameter to be named 'photo'
			fileReference.upload( request, "photo" );
		}
		/**
		 * 
		 * @param event
		 * 
		 */		
		protected function result(event:DataEvent):void
		{
			event.currentTarget.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA,result);
			
			var file:File = event.currentTarget as File;
			LocalUser.getInstance().user.bandwidthUsed += file.size;
			
			var xml:XML = new XML(event.data)
			
			var photo:Photo = new Photo();
			photo.id = xml.photoid;
			
			LocalUser.getInstance().photoCollection.addItemAt(photo,0);
			
			Logger.debug("Upload Executed");
			
			var getInfoEvent:PhotoEvent = new PhotoEvent(PhotoEvent.GET_INFO,[photo.id]);
			getInfoEvent.data = photo;
			CairngormEventDispatcher.getInstance().dispatchEvent(getInfoEvent);
		}
	}
}